
class label:
    def __init__(self, label_name, label_id, value):
        self.label_name = label_name
        self.label_id = label_id
        self.value = value


class table:
    """主表信息"""
    def __init__(self, table_name):
        self.table_name = table_name

    def __init__(self, table_name, field_name):
        self.table_name = table_name
        self.field_name = field_name

class left_table:
    """left  关联表"""
    def __init__(self, table_name, join_field, sub_filter):
        self.table_name = table_name
        self.join_field = join_field
        self.sub_filter = sub_filter

    def __str__(self):
        left_str = ' left join '+self.table_name+' on '+self.join_field
        if self.sub_filter is not None:
            left_str = left_str + ' and ' + self.join_field
        return left_str


class check_field:
    """ 0  null
        1  ''
    """
    def __init__(self, field, type, regex):
        self.field = field
        self.type = type
        self.regex = regex
        self.desc = ''

    def __str__(self):
        check_str = ''
        if type == 0:
            self.desc = str(self.field) + '为NULL;'
            check_str = ' and ' + str(self.field) + ' is null'
        if type == 1:
            self.desc = str(self.field) + '长度为0;'
            check_str = " and " + str(self.field) + "= '' "
        return check_str

    def get_desc(self):
        return self.desc

class sqlCheck:
    """# 自定义模式 构建检查sql
    check_fields（用户判断的字段 ） field =''
    master_table  主表
    left_tables 包含：表   关联字段   附加判断（包括关联条件）
    filters 包含 表名.字段名=xxx (直接拼接在最终sql的where条件下，所以要保证能正常运行 [table.filed='xxx' [and]]* )
    最终结果为
    select count(1)
    from master_table
    [left join left_tables on 关联字段 and  附加判断] *
    where 1=1
    [and check_fields]*
    [and filters]*
    """
    def __init__(self, master_table, left_tables, check_fields, filters):
        self.master_table = master_table
        self.left_tables = left_tables
        self.check_fields = check_fields
        self.filters = filters

    def get_rule_detail(self):
        rule_detail = []
        for i in range(2):
            detail_sql = 'select count(1) from ' + self.master_table.table_name
            note = ''
            category = 'denominator'
            for left_table in self.left_tables:
                detail_sql = detail_sql + str(left_table)
            detail_sql = detail_sql + ' where 1=1 '
            # 检查字段
            if i == 0:
                category = 'numerator'
                for check_field in self.check_fields:
                    note = note + check_field.get_desc()
                    detail_sql = detail_sql + str(check_field)
            # 过滤字段
            for fil in self.filters:
                detail_sql = detail_sql + str(fil)
            # 只有一个 错误数据  一个总数据   所以可以直接定义为1
            rule_detail.append((note, detail_sql, category, 1))
